package com.xbongbong.paas.controller.mobile.workflow;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.workflow.task.dto.WorkflowCommentAddDTO;
import com.xbongbong.paas.pojo.workflow.task.dto.WorkflowCommentDeleteDTO;
import com.xbongbong.paas.pojo.workflow.task.dto.WorkflowCommentEditDTO;
import com.xbongbong.paas.pojo.workflow.task.vo.WorkflowCommentAddVO;
import com.xbongbong.paas.pojo.workflow.task.vo.WorkflowCommentDeleteVO;
import com.xbongbong.paas.service.workflow.comment.WorkflowCommentService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: chenpeng
 * @date: 2021年5月17日13:58:45
 * @desrcption: 评论操作
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/workflow/comment")
public class MobileWorkflowCommentController {

    @Resource
    private WorkflowCommentService workflowCommentService;

    /**
     * 评论流程
     * @param
     * @param br
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String commentProcess(@RequestBody @Valid WorkflowCommentAddDTO workflowCommentAddDTO, BindingResult br) {
        XbbResponse<WorkflowCommentAddVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowCommentAddVO workflowCommentAddVO = workflowCommentService.add(workflowCommentAddDTO);
                response = new XbbResponse<>(workflowCommentAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 评论删除流程
     * @param
     * @param br
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String deleteComment(@RequestBody @Valid WorkflowCommentDeleteDTO workflowCommentDeleteDTO, BindingResult br) {
        XbbResponse<WorkflowCommentDeleteVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowCommentDeleteVO workflowCommentDeleteVO = workflowCommentService.delete(workflowCommentDeleteDTO);
                response = new XbbResponse<>(workflowCommentDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 工单动态编辑流程
     * @param
     * @param br
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String editComment(@RequestBody @Valid WorkflowCommentEditDTO workflowCommentEditDTO, BindingResult br) {
        XbbResponse<WorkflowCommentAddVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowCommentAddVO workflowCommentAddVO = workflowCommentService.edit(workflowCommentEditDTO);
                response = new XbbResponse<>(workflowCommentAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
